<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="存储桶名称" label-width="10" prop="bucketName">
        <el-input
          v-model="queryParams.bucketName"
          placeholder="请输入存储桶名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime">
        <el-date-picker clearable
                        v-model="queryParams.uploadTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <MinioImageUpload>

        </MinioImageUpload>
        <!-- 使用MinioUpload子组件作为上传按钮 -->
        <MinioFileUpload
          button-text="上传文件"
          button-type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          v-hasPermi="['system:minio:add']"
          :show-bucket-input="false"
          :limit="1"
          accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.xls,.xlsx,.txt"
          :file-size-limit="50"
          @upload-success="handleUploadSuccess"
          @upload-error="handleUploadError"
        />
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件名" align="center" prop="fileName" show-overflow-tooltip />
      <el-table-column label="文件大小" align="center" prop="fileSize" />
      <el-table-column label="MIME类型" align="center" prop="fileType" />
      <el-table-column label="扩展名" align="center" prop="fileExtension" />
      <el-table-column label="存储路径" align="center" prop="storagePath" show-overflow-tooltip />
      <el-table-column label="存储桶名称" align="center" prop="bucketName" />
      <el-table-column label="上传用户" align="center" prop="uploadUser" />
      <el-table-column label="上传时间" align="center" prop="uploadTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            v-hasPermi="['system:minio:download']"
          >下载</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMinioFile, removeMinioFile } from "@/api/system/minio"

export default {
  name: "MinioFile",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // MinIO文件存储表格数据
      fileList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileMd5: null,
        fileName: null,
        fileSize: null,
        fileType: null,
        originalFilename: null,
        bucketName: null,
        fileExtension: null,
        storagePath: null,
        uploadUserId: null,
        uploadTime: null,
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询MinIO文件存储列表 */
    getList() {
      this.loading = true
      listMinioFile(this.queryParams).then(response => {
        this.fileList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除MinIO文件存储编号为"' + ids + '"的数据项？').then(function() {
        return removeMinioFile(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 处理上传成功 */
    handleUploadSuccess(data) {
      this.$modal.msgSuccess("文件上传成功")
      this.getList() // 重新查询列表
    },
    /** 处理上传失败 */
    handleUploadError(error) {
      this.$modal.msgError(`文件上传失败: ${error.msg || error.message || '未知错误'}`)
    },
    /** 下载文件 */
    handleDownload(row) {
      this.minioDownload(row.fileName, row.originalFilename).catch(() => {})
    }
  }
}
</script>
